﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PMisuseAn.io;

namespace PMisuseAn
{
    /// <summary>
    /// Interaction logic for CloneAsWindow.xaml
    /// </summary>
    public partial class CloneAsWindow : Window
    {
        public ContextInterface ci_;
        string originalMachineName_;
        public CloneAsWindow(ContextInterface ci, string originalMachineName)
        {
            InitializeComponent();
            ci_ = ci;
            originalMachineName_ = originalMachineName;
            machine_name_tb.Text = originalMachineName_;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string machine_name = machine_name_tb.Text;
            machine_name = machine_name.Trim();
            machine_name = machine_name.TrimEnd('\n');
            machine_name = machine_name.TrimEnd('\r');

            if (machine_name.Count() == 0)
            {
                MessageBoxResult result = MessageBox.Show("NULL machine name...",
                            "error", MessageBoxButton.OK,
                            MessageBoxImage.Information);
                return;
            }

            List<string> machines = MisUseAnObjectsDB.loadAllmachines(MainWindow.clusterName);
            bool invalidName = false;
            foreach (string m in machines)
            {
                if (m.Equals(machine_name))
                {
                    invalidName = true;
                }
            } 
            if (invalidName)
            {
                MessageBoxResult result = MessageBox.Show("Duplicate machine names not allowed",
                            "error", MessageBoxButton.OK,
                            MessageBoxImage.Information);
                return;
            }

            ci_.cloneMachineName = machine_name;

            this.Close();
        }
    }
}
